package com.cq.hd.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cq.hd.common.enums.biz.NotifyMsgTypeEnum;
import com.cq.hd.common.pagination.Page;
import com.cq.hd.common.response.Throw;
import com.cq.hd.common.utils.BeanUtils;
import com.cq.hd.system.api.dto.NotifyMsgPageDto;
import com.cq.hd.common.entity.mq.NotifyMsgSendDto;
import com.cq.hd.system.api.dto.NotifyMsgUpdateReadDto;
import com.cq.hd.system.api.vo.NotifyMsgPageVo;
import com.cq.hd.system.po.TbNotifyMsgPo;
import com.cq.hd.system.mapper.TbNotifyMsgMapper;
import com.cq.hd.system.service.TbNotifyMsgService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 消息通知表 服务实现类
 * </p>
 *
 * @author mYunYu
 * @since 2024-01-15
 */
@Service
public class TbNotifyMsgServiceImpl extends ServiceImpl<TbNotifyMsgMapper, TbNotifyMsgPo> implements TbNotifyMsgService {

    @Override
    public Boolean saveNotifyMsg(NotifyMsgSendDto notifyMsgSaveDto) {
        TbNotifyMsgPo tbNotifyMsgPo = new TbNotifyMsgPo();
        BeanUtils.copyProperties(notifyMsgSaveDto, tbNotifyMsgPo);
        tbNotifyMsgPo.setNotifyTime(LocalDateTime.now());
        tbNotifyMsgPo.setCreateTime(LocalDateTime.now());
        int insert = baseMapper.insert(tbNotifyMsgPo);
        if (insert == 0) {
            Throw.isBusinessException("保存通知消息失败");
        }

        return true;
    }

    @Override
    public Page<NotifyMsgPageVo> pageNotifyMsg(NotifyMsgPageDto notifyMsgPageDto) {
        PageHelper.startPage(notifyMsgPageDto.getPageNum(), notifyMsgPageDto.getPageSize());
        List<NotifyMsgPageVo> notifyMsgPageVos = baseMapper.listNotifyMsgByDto(notifyMsgPageDto);
        if (!CollectionUtils.isEmpty(notifyMsgPageVos)) {
            for (NotifyMsgPageVo notifyMsgPageVo : notifyMsgPageVos) {
                NotifyMsgTypeEnum notifyMsgTypeEnum = NotifyMsgTypeEnum.typeOfValue(notifyMsgPageVo.getMsgType());
                if (notifyMsgTypeEnum != null) {
                    notifyMsgPageVo.setMsgTitle(notifyMsgTypeEnum.getName());
                }
            }
        }
        PageInfo<NotifyMsgPageVo> pageInfo = new PageInfo<>(notifyMsgPageVos);
        return Page.newPageWithAllFields(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getPages());
    }

    @Override
    public Boolean updateRead(NotifyMsgUpdateReadDto notifyMsgUpdateReadDto) {
        Long id = notifyMsgUpdateReadDto.getId();
        if (id == null) {
            Throw.isBusinessException("消息id不能为空");
        }

        TbNotifyMsgPo tbNotifyMsgPo = baseMapper.selectById(id);
        if (tbNotifyMsgPo == null) {
            Throw.isBusinessException("未找到消息数据");
        }

        if (!tbNotifyMsgPo.getRoleType().equals(notifyMsgUpdateReadDto.getRoleType())) {
            Throw.isBusinessException("未找到消息数据");
        }

        if (!tbNotifyMsgPo.getUserId().equals(notifyMsgUpdateReadDto.getUserId())) {
            Throw.isBusinessException("未找到消息数据");
        }

        // 消息状态（1-未读，2-已读）
        if (tbNotifyMsgPo.getMsgState() == 2) {
            return true;
        }

        tbNotifyMsgPo.setMsgState(2);
        tbNotifyMsgPo.setReadTime(LocalDateTime.now());
        tbNotifyMsgPo.setUpdateTime(LocalDateTime.now());
        baseMapper.updateById(tbNotifyMsgPo);

        return true;
    }

    @Override
    public Boolean cleanNoRead(NotifyMsgUpdateReadDto notifyMsgUpdateReadDto) {
        notifyMsgUpdateReadDto.setMsgState(2);
        notifyMsgUpdateReadDto.setReadTime(LocalDateTime.now());
        notifyMsgUpdateReadDto.setUpdateTime(LocalDateTime.now());
        baseMapper.cleanNoRead(notifyMsgUpdateReadDto);
        return true;
    }

    @Override
    public Integer getNoReadCount(NotifyMsgPageDto notifyMsgPageDto) {
        return baseMapper.selectCount(new QueryWrapper<TbNotifyMsgPo>().eq("del_state", 0)
                .eq("msg_state", 1)
                .eq("user_id", notifyMsgPageDto.getUserId())
                .eq("role_type", notifyMsgPageDto.getRoleType()));
    }
}
